package com.example.assisteddriving.service;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;


import com.example.assisteddriving.R;
import com.example.assisteddriving.bean.Drivelog_;
import com.example.assisteddriving.bean.Xinlvlog;
import com.example.assisteddriving.dao.DrivelogDao;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class AnalyseSevice extends Service {
    ServerSocket ss;
    Socket socket;
    Thread analysethread,jishithread;
    Runnable analysetask;
    public static final String PREFERENCE_NAME = "log";  //文件名
    public static int MODE = MODE_PRIVATE;  //操作模式

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        analysetask=new Runnable() {
            @Override
            public void run() {
                avgtime();
                if (averagesleeptime>averagetiredtime){
                    try {
                        Thread.sleep(averagetiredtime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    MediaPlayer mediaPlayer;
                    mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.yugaopilao);
                    if (!mediaPlayer.isPlaying()) mediaPlayer.start();
                    else if (mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                        mediaPlayer.start();
                    }


                    try {
                        Thread.sleep(averagesleeptime-averagetiredtime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.yugaoshuimian);
                    if (!mediaPlayer.isPlaying()) mediaPlayer.start();
                    else if (mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                        mediaPlayer.start();
                    }

                }else {
                    try {
                        Thread.sleep(averagesleeptime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    MediaPlayer mediaPlayer;
                    mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.yugaoshuimian);
                    if (!mediaPlayer.isPlaying()) mediaPlayer.start();
                    else if (mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                        mediaPlayer.start();
                    }


                    try {
                        Thread.sleep(averagetiredtime-averagesleeptime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.yugaopilao);
                    if (!mediaPlayer.isPlaying()) mediaPlayer.start();
                    else if (mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                        mediaPlayer.start();
                    }
                }

            }
        };
        analysethread=new Thread(analysetask);
    }

    private void avgxl() {
        List<Xinlvlog> list=new ArrayList<>();
        DrivelogDao drivelogDao=new DrivelogDao(AnalyseSevice.this);
        list=drivelogDao.getxl();
        float xl,tw,xy;
        int A=0,B=0,C=0;
        if (list.size()!=0) {
            for (int i=0;i<list.size();i++){
                float a= Float.parseFloat(list.get(i).getXinlv())*100;
                float b=Float.parseFloat(list.get(i).getTiwen())*100;
                float c=Float.parseFloat(list.get(i).getXueyang())*100;
                A=(int)a+A;
                B=(int)b+B;
                C=(int)c+C;
                xl=A/100;
            }
            A=(int) A/list.size();
            B=(int) B/list.size();
            C=(int) C/list.size();
            xl=A/100;
            tw=B/100;
            xy=C/100;
        }
    }

    long averagetiredtime = 0,averagesleeptime = 0;
    public void avgtime(){
        SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
        DrivelogDao drivelogDao=new DrivelogDao(AnalyseSevice.this);
        List<Drivelog_> list=new ArrayList<>();
        int n=sharedPreferences.getInt("log",0);
        long time0 = 0,tiredtime1,sleeptime1;
        long t=0,s=0;

        if(n>0){
            for (int i=1;i<=n;i++){
                list= drivelogDao.getlog(String.valueOf(i));
                if(list!=null){
                    for (int j=0;j<list.size();j++){
                        if (list.get(j).getTired().equals("0")&&list.get(j).getSleep().equals("0")){
                            time0=list.get(j).getTime();
                        }
                        if (list.get(j).getTired().equals("1")){
                            tiredtime1=list.get(j).getTime();
                            averagetiredtime=tiredtime1-time0;
                            t++;
                        }
                        if (list.get(j).getSleep().equals("1")){
                            sleeptime1=list.get(j).getTime();
                            averagesleeptime=averagesleeptime+sleeptime1-time0;
                            s++;
                        }

                    }
                }
            }
            if(t != 0 && s != 0){
                averagetiredtime=averagetiredtime/t;
                averagesleeptime=averagesleeptime/s;
            }

            Log.v("asd", "平均睡眠时间"+String.valueOf(averagesleeptime));
            Log.v("asd", "平均疲劳时间"+String.valueOf(averagetiredtime));
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(!analysethread.isAlive()){
            analysethread.start();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(analysethread.isAlive()) {
            Log.v("asd", "语音服务已结束");
        }
    }
}
